package com.isoft.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isoft.entity.SharedFlights;
import com.isoft.result.R;
import com.isoft.service.SharedFlightsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sharedFlights")
public class SharedFlightsController {

    @Autowired
    private SharedFlightsService sharedFlightsService;

    /**
     * 分页查询共享航班信息
     */
    @GetMapping("/page")
    public R getPage(@RequestParam(defaultValue = "1") Integer current,
                     @RequestParam(defaultValue = "10") Integer size,
                     @RequestParam(required = false) Long flightId,
                     @RequestParam(required = false) String sfaw) {
        Page<SharedFlights> page = new Page<>(current, size);
        QueryWrapper<SharedFlights> queryWrapper = new QueryWrapper<>();

        if (flightId != null) {
            queryWrapper.eq("flight_id", flightId);
        }
        if (sfaw != null && !sfaw.isEmpty()) {
            queryWrapper.like("sfaw", sfaw);
        }

        IPage<SharedFlights> pageResult = sharedFlightsService.page(page, queryWrapper);
        return R.ok(pageResult);
    }

    /**
     * 获取所有共享航班信息
     */
    @GetMapping("/list")
    public R getList() {
        List<SharedFlights> list = sharedFlightsService.list();
        return R.ok(list);
    }

    /**
     * 根据ID获取共享航班信息
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable Long id) {
        SharedFlights sharedFlights = sharedFlightsService.getById(id);
        return R.ok(sharedFlights);
    }

    /**
     * 新增共享航班信息
     */
    @PostMapping
    public R save(@RequestBody SharedFlights sharedFlights) {
        boolean result = sharedFlightsService.save(sharedFlights);
        return result ? R.ok("新增成功") : R.error("新增失败");
    }

    /**
     * 更新共享航班信息
     */
    @PutMapping
    public R update(@RequestBody SharedFlights sharedFlights) {
        boolean result = sharedFlightsService.updateById(sharedFlights);
        return result ? R.ok("更新成功") : R.error("更新失败");
    }

    /**
     * 删除共享航班信息
     */
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Long id) {
        boolean result = sharedFlightsService.removeById(id);
        return result ? R.ok("删除成功") : R.error("删除失败");
    }

    /**
     * 批量删除共享航班信息
     */
    @DeleteMapping("/batch")
    public R deleteBatch(@RequestBody List<Long> ids) {
        boolean result = sharedFlightsService.removeByIds(ids);
        return result ? R.ok("批量删除成功") : R.error("批量删除失败");
    }
}